1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.henri.jbcs;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 import com.borland.jbuilder.node.JavaFileNode;
32 import com.borland.primetime.ide.Message;
33 import com.puppycrawl.tools.checkstyle.Checker;
34 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
35 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
36
37 /***
38 * Thread to run the Checkstyle <code>Checker</code> in.
39 *
40 * @author Henri Tremblay
41 * @version $Revision: 1.4 $ $Date: 2005/08/08 23:55:30 $
42 */
43 class CheckerThread extends Thread
44 {
45 /*** Map containing file names and corresponding JavaFileNodes */
46 private Map m_files;
47
48 /*** Message handler for this thread */
49 MessageHandler m_msgHandler;
50
51 /***
52 * Default constructor
53 *
54 * @param nodes Array of JavaFile nodes to fun CheckStyle on.
55 */
56
57 CheckerThread(JavaFileNode[] nodes, MessageHandler msgHandler) {
58 m_files = new HashMap(nodes.length * 2);
59 for(int i = 0; i < nodes.length; i++) {
60 m_files.put(nodes[i].getUrl().getFullName(), nodes[i]);
61 }
62 m_msgHandler = msgHandler;
63 }
64
65 /***
66 * {@inheritDoc}
67 */
68 public void run() {
69
70
71 if(CheckStyleOpenTool.getPreference().getClearMessagePane()) {
72 m_msgHandler.clearMessages();
73 }
74
75
76 File[] files = new File[m_files.size()];
77 Iterator it = m_files.keySet().iterator();
78 for(int i = 0; i < files.length; i++) {
79 String fileName = (String) it.next();
80 files[i] = new File(fileName);
81
82 if(((JavaFileNode) m_files.get(fileName)).isModified()) {
83 try {
84 ((JavaFileNode) m_files.get(fileName)).save();
85 }
86 catch(IOException e) {
87 m_msgHandler.addMessage("Unable to save " + fileName + " " + e.getMessage() + " --> Aborting");
88 return;
89 }
90 }
91 }
92
93
94 Checker checker = CheckStyleOpenTool.getChecker();
95 if(checker == null) {
96 return;
97 }
98 FailureCountListener auditListener = new FailureCountListener()
99 {
100
101 /***
102 * List of CheckStyleMessage found for a file
103 */
104 private List m_messageList = new ArrayList();
105
106 public void auditStarted(AuditEvent evt) {
107 m_msgHandler.writeStatusBarMessage("Checkstyle is running...");
108 super.auditStarted(evt);
109 }
110
111 public void auditFinished(AuditEvent evt) {
112 super.auditFinished(evt);
113
114 int totalErrorNumber = getTotalFailureCount(SeverityLevel.ERROR);
115
116 m_msgHandler.writeStatusBarMessage("Checkstyle completed - " + totalErrorNumber + " Error(s) found");
117 }
118
119 public void fileStarted(AuditEvent evt) {
120 super.fileStarted(evt);
121 m_messageList.clear();
122 }
123
124 public void fileFinished(AuditEvent evt) {
125 super.fileFinished(evt);
126
127 if(m_messageList.size() != 0 || CheckStyleOpenTool.getPreference().getShowNoError()) {
128 Message parentMsg = m_msgHandler.addMessage(evt.getFileName());
129 m_msgHandler.addMessages(parentMsg, (CheckStyleMessage[]) m_messageList
130 .toArray(new CheckStyleMessage[m_messageList.size()]));
131 }
132 }
133
134 public void addError(AuditEvent evt) {
135 super.addError(evt);
136
137
138 if(CheckStyleOpenTool.getPreference().isSeverityShown(evt.getSeverityLevel())) {
139 m_messageList.add(new CheckStyleMessage((JavaFileNode) m_files
140 .get(CheckStyleOpenTool.getBasedir() + evt.getFileName()), evt.getLine(),
141 evt.getMessage(), evt.getSeverityLevel()));
142 }
143 }
144
145 public void addException(AuditEvent evt, Throwable ex) {
146 super.addException(evt, ex);
147
148 m_messageList.add(new CheckStyleMessage((JavaFileNode) m_files.get(evt.getFileName()), evt.getLine(), evt.getMessage(),
149 SeverityLevel.ERROR));
150 m_msgHandler.error(evt.getMessage(), ex);
151 }
152 };
153
154 checker.addListener(auditListener);
155 try {
156 checker.process(files);
157 } catch(RuntimeException e) {
158 throw e;
159 } finally {
160 checker.removeListener(auditListener);
161 }
162 }
163 }